package com.xbai.goods.controller;

import com.github.pagehelper.PageInfo;
import com.xbai.entity.Result;
import com.xbai.entity.StatusCode;
import com.xbai.goods.pojo.Goods;
import com.xbai.goods.pojo.Spu;
import com.xbai.goods.service.SpuService;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author jxbai
 * @desc SpuController
 * @date 2021/1/28
 */
@RestController
@RequestMapping("/spu")
@CrossOrigin
@RequiredArgsConstructor
public class SpuController {

    private final SpuService spuService;

    @PutMapping("/restore/{id}")
    public Result restore(@PathVariable("id") Long spuId) {
        spuService.restore(spuId);
        return new Result(true,StatusCode.OK,"数据恢复成功！");
    }

    @DeleteMapping("/logic/delete/{id}")
    public Result  logicDelete(@PathVariable("id") Long spuId) {
        spuService.logicDelete(spuId);
        return new Result(true,StatusCode.OK,"逻辑删除成功！");
    }

    @PutMapping("/pull/many")
    public Result pullMany(@RequestBody Long[] ids) {
        int count = spuService.pullMany(ids);
        return new Result(true, StatusCode.OK, "下架" + count + "个商品");
    }

    @PutMapping("/put/many")
    public Result putMany(@RequestBody Long[] ids) {
        int count = spuService.putMany(ids);
        return new Result(true, StatusCode.OK, "上架" + count + "个商品");
    }

    @PutMapping("/put/{id}")
    public Result put(@PathVariable("id") Long spuId) {
        spuService.put(spuId);
        return new Result();
    }

    @PutMapping("/pull/{id}")
    public Result pull(@PathVariable("id") Long spuId) {
        spuService.pull(spuId);
        return new Result();
    }

    @PutMapping("/audit/{id}")
    public Result audit(@PathVariable("id") Long spuId) {
        spuService.audit(spuId);
        return new Result();
    }

    @GetMapping("/goods/{id}")
    public Result<Goods> findGoodsById(@PathVariable("id") Long spuId) {
        Goods goods = spuService.findGoodsById(spuId);
        return new Result<>(goods);
    }

    @PostMapping("/save")
    public Result saveGoods(@RequestBody Goods goods) {
        spuService.saveGoods(goods);
        return new Result();
    }

    /***
     * Spu分页条件搜索实现
     * @param spu {@link Spu}
     * @param page 页码
     * @param size 页大小
     * @return {@link Result<PageInfo>}
     */
    @PostMapping(value = "/search/{page}/{size}")
    public Result<PageInfo> findPage(@RequestBody(required = false) Spu spu, @PathVariable int page, @PathVariable int size) {
        //调用SpuService实现分页条件查询Spu
        PageInfo<Spu> pageInfo = spuService.findPage(spu, page, size);
        return new Result<>(pageInfo);
    }

    /***
     * Spu分页搜索实现
     * @param page 页码
     * @param size 页大小
     * @return {@link Result<PageInfo>}
     */
    @GetMapping(value = "/search/{page}/{size}")
    public Result<PageInfo> findPage(@PathVariable int page, @PathVariable int size) {
        //调用SpuService实现分页查询Spu
        PageInfo<Spu> pageInfo = spuService.findPage(page, size);
        return new Result<>(pageInfo);
    }

    /***
     * 多条件搜索Spu数据
     * @param spu {@link Spu}
     * @return {@link Result}
     */
    @PostMapping(value = "/search")
    public Result<List<Spu>> findList(@RequestBody(required = false) Spu spu) {
        //调用SpuService实现条件查询Spu
        List<Spu> list = spuService.findList(spu);
        return new Result<>(list);
    }

    /***
     * 根据ID删除Spu数据
     * @param id 主键id
     * @return {@link Result}
     */
    @DeleteMapping(value = "/{id}")
    public Result delete(@PathVariable Long id) {
        //调用SpuService实现根据主键删除
        spuService.delete(id);
        return new Result();
    }

    /***
     * 修改Spu数据
     * @param spu {@link Spu}
     * @param id 主键id
     * @return {@link Result}
     */
    @PutMapping(value = "/{id}")
    public Result update(@RequestBody Spu spu, @PathVariable Long id) {
        //设置主键值
        spu.setId(id);
        //调用SpuService实现修改Spu
        spuService.update(spu);
        return new Result();
    }

    /***
     * 新增Spu数据
     * @param spu {@link Spu}
     * @return {@link Result}
     */
    @PostMapping
    public Result add(@RequestBody Spu spu) {
        //调用SpuService实现添加Spu
        spuService.add(spu);
        return new Result();
    }

    /***
     * 根据ID查询Spu数据
     * @param id 主键id
     * @return {@link Result<Spu>}
     */
    @GetMapping("/{id}")
    public Result<Spu> findById(@PathVariable(name = "id") Long id) {
        //调用SpuService实现根据主键查询Spu
        Spu spu = spuService.findById(id);
        return new Result<>(spu);
    }

    /***
     * 查询Spu全部数据
     * @return {@link Result}
     */
    @GetMapping
    public Result<List<Spu>> findAll() {
        //调用SpuService实现查询所有Spu
        List<Spu> list = spuService.findAll();
        return new Result<>(list);
    }
}
